package com.meixiang.activity.account;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.google.gson.reflect.TypeToken;
import com.meixiang.R;
import com.meixiang.adapter.AllPrivilegeAdapter;
import com.meixiang.entity.account.AllPrivilegeEntity;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseActivity;
import com.meixiang.recyclerview.OnItemClick;
import com.meixiang.util.AbJsonUtil;
import com.meixiang.util.AbStrUtil;
import com.meixiang.util.AbToastUtil;
import com.meixiang.view.swipetoloadlayout.base.SwipeToLoadLayout;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 全部特权
 * author：wangzuzhen on 2016/5/6 0006 10:33.
 */
public class AllPrivilegeActivity extends BaseActivity {


    @Bind(R.id.swipe_target)
    RecyclerView mSwipeTarget;
    @Bind(R.id.refresh)
    SwipeToLoadLayout mRefresh;

    private AllPrivilegeAdapter mAdapter;
    private String strStatus;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_all_privilege);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initTitle() {
        setTitle("全部特权");
        strStatus = getIntent().getStringExtra("status");
        if (!AbStrUtil.isEmpty(strStatus)) {
            setTitle(strStatus);
        }
    }

    @Override
    protected void initView() {
        mRefresh.setLoadMoreEnabled(false);
        mRefresh.setRefreshEnabled(false);

        mAdapter = new AllPrivilegeAdapter(context);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(context);
        mSwipeTarget.setLayoutManager(layoutManager);
        mSwipeTarget.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new OnItemClick() {
            @Override
            public void onItemClick(View view, int position) {


            }
        });
    }

    @Override
    protected void setData() {
        getAllPrivilegeData();
    }

    private void getAllPrivilegeData() {

        HttpUtils.post(Config.GET_ALL_PRIVILEGE, null, new HttpCallBack(AllPrivilegeActivity.this) {

            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                status.removeView();
                if ("0".equals(code)) {
                    List<AllPrivilegeEntity> mList = AbJsonUtil.fromJson(result.optJSONArray("list").toString(), new TypeToken<ArrayList<AllPrivilegeEntity>>(){});
                    if (mList.size()>0) {
                        mAdapter.refreshData(mList);
                    }else {
                        status.showNoData("暂无数据");
                    }
                }else {
                    AbToastUtil.showToast(context, message);
                }
            }

            @Override
            public void onError(String code, String message) {
                AbToastUtil.showToast(context, message);
            }
        });

    }


    @Override
    public void onClick(View v) {

    }
}
